/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 * 广度优先遍历，记录每一层的节点个数，出队之后让长度减1，当当前层长度等于1时 说明是最边的节点。
 */
 var rightSideView = function (root) {
    let res = []
    if (!root) return res
    let q = [root]
    while (q.length !== 0) {
      let len = q.length
      while (len) {
        let node = q.shift()
        if (len === 1) res.push(node.val)
        if (node.left) q.push(node.left)
        if (node.right) q.push(node.right)
        len--
      }
    }
    return res
  };